package com.wcgksoft.controller.greedLand;

import com.google.gson.JsonObject;
import com.wcgksoft.common.util.FileHandleUtil;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.GreedLand.GreedLand;
import com.wcgksoft.entity.GreedLand.GreedLandAdministrator;
import com.wcgksoft.entity.GreedLand.GreedLandPhoto;
import com.wcgksoft.entity.Plant.Plant;
import com.wcgksoft.entity.Plant.PlantEncyclopedias;
import com.wcgksoft.entity.mainten.Mainten;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.GreedLand.GreedlandAdministratorService;
import com.wcgksoft.service.GreedLand.GreedlandPhotoService;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.mainten.MaintenService;
import com.wcgksoft.service.plant.PlantEncyclopediasService;
import com.wcgksoft.service.plant.PlantService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 绿地管理
 *
 * @return
 */
@Controller
@RequestMapping(value = "greedLand")
public class GreedLandController {
    @Autowired
    private GreedlandService greedlandService;
    @Autowired
    private GreedlandAdministratorService greedlandAdministratorService;
    @Autowired
    private PlantService plantService;
    @Autowired
    private MaintenService maintenService;

    @Autowired
    private PlantEncyclopediasService plantEncyclopediasService;
    @Autowired
    private GreedlandPhotoService greedlandPhotoService;

    /**
     * 绿地管理列表-
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "keyWord", required = false) String keyWord) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        if(StringUtils.isNotEmpty(keyWord)) {
            criteria.createAlias("greedLandAdministrator", "greedLandAdministrator");
            criteria.add(Restrictions.like("greedLandAdministrator.name", keyWord, MatchMode.ANYWHERE));
        }
        List<GreedLand> list = greedlandService.findByConditions(criteria, page, limit);
        for (GreedLand greedLand : list) {
            DetachedCriteria criteria2 = DetachedCriteria.forClass(GreedLandPhoto.class);
            criteria2.add(Restrictions.eq("greedId",greedLand.getId()));
            List<GreedLandPhoto> byConditions = greedlandPhotoService.findByConditions(criteria2);
            greedLand.setGreedLandPhotoList(byConditions);
        }
        return Layui.data(0, "查询成功", greedlandService.getTotalCount(), list);
    }

    /*
       查询绿地名称
       */
    @ResponseBody
    @RequestMapping(value = "getGreedLandName")
    public List<GreedLandAdministrator> getGreedLandName() {
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLandAdministrator.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()));
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()));
        criteria.add(Restrictions.eq("isuse", 0));
        criteria.add(Restrictions.eq("state", 0));
        List<GreedLandAdministrator> list = greedlandAdministratorService.findByConditions(criteria);
        return list;
    }
    /*
     查询绿地名称修改
     */
    @ResponseBody
    @RequestMapping(value = "getGreedLandNamex")
    public List<GreedLandAdministrator> getGreedLandNamex() {
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLandAdministrator.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()));
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()));
        List<GreedLandAdministrator> list = greedlandAdministratorService.findByConditions(criteria);
        return list;
    }



    /*
     * 修改
     *
     * */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(HttpServletRequest request, @RequestParam(value = "data", required = false) String data,
                        @RequestParam(value = "file", required = false) MultipartFile[] file
    ) throws Exception {
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            GreedLand greenLand = (GreedLand) JSONObject.toBean(JSONObject.fromObject(data), GreedLand.class);
            String ids = String.valueOf(param.get("id"));
            String greedLandAdministrator = String.valueOf(param.get("greedLandAdministrator_id"));
            Integer greedLandAdministrator_id=Integer.valueOf(greedLandAdministrator);
            GreedLandAdministrator byId = greedlandAdministratorService.findById(greedLandAdministrator_id);

            if (ids.equals("")) {
                greenLand.setState(0);
                //使用
                byId.setIsuse(1);
                greedlandAdministratorService.upadte(byId);

            }
            for (MultipartFile mf : file) {
                GreedLandPhoto greedLandPhoto = new GreedLandPhoto();
                if (!mf.isEmpty()) {
                    String filename = FileHandleUtil.Upset(mf);
                    greedLandPhoto.setPhoto("upload/picture/" + FileHandleUtil.upload(mf.getInputStream(), "upload/picture/", filename, request));
                    greedLandPhoto.setGreedId(greenLand.getId());
                    greedLandPhoto.setTime(new Date());
                    greedlandPhotoService.saveOrUpdate(greedLandPhoto);
                }
            }
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
            EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
            greenLand.setDepartid(sysUser.getProId()+"");
            greenLand.setAreaid(enterpriseProject.getId()+"");
            greenLand.setEnterpriseid(sysUser.getEnterprise().getId());
            greenLand.setGreedLandAdministrator(byId);
            greedlandService.saveOrUpdate(greenLand);
            return Layui.data(0, "修改成功", 1, Layui.addList(greenLand));
        }
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="stop")
    public Layui stop(@RequestParam(value = "id") Integer id){
        GreedLand byId = greedlandService.findById(id);
        byId.setState(1);
        greedlandService.upadte(byId);
        return Layui.data(1,"停用",1,null);
    }

    /**
     *  启用
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="start")
    public Layui start(@RequestParam(value = "id") Integer id){
        GreedLand byId = greedlandService.findById(id);
        byId.setState(0);
        greedlandService.upadte(byId);
        return Layui.data(1,"启用",1,null);
    }

    /**
     * 绿地概览
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="view")
    public MsgInfo view(@RequestParam(value = "id") Integer id){
        MsgInfo msgInfo = new MsgInfo();
        GreedLand byId = greedlandService.findById(id);
        msgInfo.setData(byId);
        return msgInfo;
    }

    /**
     * 绿地统计
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value="statistics")
    public MsgInfo statistics(){
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        criteria.add(Restrictions.eq("state", 0));
        List<GreedLand> list = greedlandService.findByConditions(criteria);
        JsonObject test=new JsonObject();
       if(list.size()>0){
           int tjarea=0; //土建
           int lharea=0;// 绿化
           for(int i=0;i<list.size();i++){
               tjarea+=list.get(i).getSubtotalOfLandArea();
               lharea+=list.get(i).getGreenArea();

           }
           DetachedCriteria criteriae = DetachedCriteria.forClass(PlantEncyclopedias.class);
           criteriae.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
           List<PlantEncyclopedias> liste = plantEncyclopediasService.findByConditions(criteriae);

           DetachedCriteria criteriat = DetachedCriteria.forClass(Plant.class);
           criteriat.add(Restrictions.eq("departid", sysUser.getProId()+""));
           criteriat.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
           List<Plant> listt = plantService.findByConditions(criteriat);


          int plant=0;
            if(liste.size()>0){
                for(int i=0;i<liste.size();i++){
                        if(listt.size()>0){
                            for(int j=0;j<listt.size();j++){
                                    if(liste.get(i).getId().equals(listt.get(j).getPlantEncyclopedias().getId())){
                                        plant=plant+1;
                                        break;
                                    }
                            }
                        }
                }
            }
//养护数量
           DetachedCriteria criteriam = DetachedCriteria.forClass(Mainten.class);
           criteriam.add(Restrictions.eq("departid", sysUser.getProId()+""));
           criteriam.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
           List<Mainten> listm = maintenService.findByConditions(criteriam);

           test.addProperty("greed", list.size());
           test.addProperty("area", tjarea+lharea);
           test.addProperty("tjarea", tjarea);
           test.addProperty("lharea", lharea);
           test.addProperty("plant", plant);
           test.addProperty("yhnum", listm.size());

       }

       msgInfo.setData(test.toString());
        return msgInfo;
    }
    /**
     * 绿地分类统计
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value="typestatistics")
    public MsgInfo typestatistics(){
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        criteria.add(Restrictions.eq("state", 0));
        criteria.add(Restrictions.eq("type", "0"));
        List<GreedLand> list = greedlandService.findByConditions(criteria);

        DetachedCriteria criteria1 = DetachedCriteria.forClass(GreedLand.class);
        criteria1.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria1.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        criteria1.add(Restrictions.eq("state", 0));
        criteria1.add(Restrictions.eq("type", "1"));
        List<GreedLand> list1 = greedlandService.findByConditions(criteria1);

        DetachedCriteria criteria2 = DetachedCriteria.forClass(GreedLand.class);
        criteria2.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria2.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        criteria2.add(Restrictions.eq("state", 0));
        criteria2.add(Restrictions.eq("type", "2"));
        List<GreedLand> list2 = greedlandService.findByConditions(criteria2);

        JsonObject test=new JsonObject();
        test.addProperty("jc", list.size());
        test.addProperty("zh", list1.size());
        test.addProperty("sq", list2.size());


        msgInfo.setData(test.toString());
        return msgInfo;
    }



    /**
     * 绿地统计企业端
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value="qystatistics")
    public MsgInfo qystatistics(){
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        criteria.add(Restrictions.eq("state", 0));
        List<GreedLand> list = greedlandService.findByConditions(criteria);



        JsonObject test=new JsonObject();
        if(list.size()>0){
            int tjarea=0; //土建
            int lharea=0;// 绿化
            for(int i=0;i<list.size();i++){
                tjarea+=list.get(i).getSubtotalOfLandArea();
                lharea+=list.get(i).getGreenArea();

            }
            test.addProperty("area", tjarea+lharea);
            test.addProperty("tjarea", tjarea);
            test.addProperty("lharea", lharea);

        }

        msgInfo.setData(test.toString());
        return msgInfo;
    }
}
